<template>
  <div class="login-page">
    <!-- 导航栏部分 -->
    <van-nav-bar title="注册"></van-nav-bar>
    <!-- 用户名和密码及提交的表单部分 -->
    <van-form @submit="onSubmit">
      <van-field v-model="username" name="username" label="用户名" placeholder="请输入用户名" :rules="[
        {required:true,message:'用户名不能为空'},
        {pattern:/^\w{5,}$/,message:'用户名至少包含5个字符'}
      ]" />
      <van-field v-model="password" type="password" name="password" label="密码" placeholder="请输入密码" :rules="[
        {required:true,message:'密码不能为空'},
        {pattern:/^\w{6,}$/,message:'密码至少包含6个字符'}
      ]"  />
      <div style="margin: 16px;">
        <!-- block: 占据整行，round：圆角效果  native-type: 设置原生 <button> 标签的 type 属性。-->
        <van-button round block type="primary" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <!-- 跳转页面部分 -->
    <router-link class="link" to="/login">有账号,去登录</router-link>
  </div>
</template>

<script>
import { register } from '@/api/user'
// import request from '@/utils/request'

export default {
  name: 'LoginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (value) {
    // try {
    //   // console.log('submit', value)
    //   const res = await request.post('/h5/user/register', value)
    //   console.log(res)
    // } catch (error) {
    //   console.log(error)
    // }
      // try {
      this.$toast.loading({
        message: '正在注册中...',
        forbidClick: true
      })
      await register(value)
      this.$toast.success('注册成功')
      this.$router.push('/login')
      // } catch (error) {
      this.$toast.fail('注册失败')
      // }
    }

  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
